import Vue from 'vue'
import VueRouter from 'vue-router'
//路由懒加载 
//懒加载简单来说就是延迟加载或按需加载，即在需要的时候的时候进行加载 
const Login = () => import('../components/Login.vue')
const Home = () => import('../components/Home.vue')
const Welcome = () => import('../components/view/welcome.vue')
const Users = () => import('../components/view/user/users.vue')
const Classify = () => import('../components/view/goods/classify.vue')
const GoodsList = () => import('../components/view/goods/goodsList.vue')
const RootUser = () => import('@/components/view/root/rootUser.vue')
const IndentList = () => import('@/components/view/indent/indentList.vue')
const DataList = () => import('@/components/view/data/dataList.vue')
Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:'/login'},
  {path:'/login',component:Login},
  {
    path:'/home',
    component:Home,
    children:[
      {
        //重定向
        path: '',
        redirect:'welcome'
      },
      {path:'welcome',component:Welcome},
      {path:'users',component:Users},
      {path:'classify',component:Classify},
      {path:'goodslist',component:GoodsList},
      {path:'root',component:RootUser},
      {path:'indent',component:IndentList},
      {path:'data',component:DataList}
    ]
  }
]

const router = new VueRouter({
  routes
})
//前置路由导航守卫
router.beforeEach((to,from,next)=>{

  if(to.path === '/login') return next();
  const key = sessionStorage.getItem('key');
  if(!key) return next('/login');
  next()

})
export default router
